package com.example.listenerproject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button popup_window_choose_bt;
    private MyPopupWindow myPopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        ActionBar actionBar = getSupportActionBar();
//        if (actionBar!=null){
//            actionBar.hide();
//        }
        //设置布局
        setContentView(R.layout.activity_main);
        //初始化控件
        Button anonymity_inner_class_listener = findViewById(R.id.anonymity_inner_class_listener);
        Button inner_class_listener = findViewById(R.id.inner_class_listener);
        Button ext_class_listener = findViewById(R.id.ext_class_listener);
        Button sef_class_listener = findViewById(R.id.sef_class_listener);
        popup_window_choose_bt = findViewById(R.id.popup_window_choose_bt);
         /*
         * 事件监听的实现
         * */
        //方式一：非静态内部类的实现
            // 点击监听的对象
        MyOnClickListener myOnClickListener = new MyOnClickListener();
            // 设置按钮监听
        inner_class_listener.setOnClickListener(myOnClickListener);


        //方式二、设置按钮监听 -该示例使用了OnClickListener匿名内部类的方式实现
        anonymity_inner_class_listener.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, InnerClassAct.class);
                startActivity(intent);
            }
        });

        //方式三：外部类监听
        ext_class_listener.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ExternalActivity.class);
                startActivity(intent);
            }
        });

        //方法四：通过MainActivity实现OnClickListener接口
        //在此MainActivity就具备了OnClickListener接口功能-onClick
        sef_class_listener.setOnClickListener(MainActivity.this);


        //点击弹窗按钮
        popup_window_choose_bt.setOnClickListener(MainActivity.this);
    }


    //方法四：通过MainActivity实现OnClickListener接口
    @Override
    public void onClick(View v) {
            int viewId = v.getId();
            if (viewId==R.id.sef_class_listener){
                Toast.makeText(this, "直接使用 Activity 作为事件监听器的按钮", Toast.LENGTH_SHORT).show();

            }else if(viewId==R.id.popup_window_choose_bt){//点击弹窗
                myPopupWindow = new MyPopupWindow(MainActivity.this);
                myPopupWindow.show(popup_window_choose_bt);
                myPopupWindow.setChooseItemPopViewClickListener(new MyPopupWindow.ChooseItemPopViewClickListener() {
                    @Override
                    public void takePicture() {
                        Toast.makeText(MainActivity.this,"点击了拍照",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void takeVideo() {
                        Toast.makeText(MainActivity.this,"点击了摄像",Toast.LENGTH_LONG).show();
                    }
                });


            }
    }


    class MyOnClickListener implements View.OnClickListener{//非静态内部类的监听

        @Override
        public void onClick(View view) {//点击事假的回调
            if (view.getId()==R.id.inner_class_listener){
                Toast.makeText(MainActivity.this,"这是非内部类监听的事件处理",Toast.LENGTH_SHORT).show();
            }


        }
    }

}


